#ifndef __BSP_UART_H__
#define __BSP_UART_H__

#include "stdarg.h"
#include "stdint.h"
#include "Bsp.h"

#define FOSC                    11059200UL

#define USART_PRINTF_BUF_SIZE   64
#define USE_USART1              1
#define USE_USART2              0

typedef enum USART_Tag {
    UART1 = 1,
    UART2,
    UART3,
    UART4
} UART_TypeDef;

typedef struct COM_Tag {
    UART_TypeDef  UARTx;

    uint8_t  *Uart_RxBuf;
    uint16_t  Uart_RxBuf_Size;
    uint16_t  Uart_Rx_Cnt;
    uint8_t   Uart_RxTimeOut;

    uint8_t  *Uart_TxBuf;
    uint16_t  Uart_TxBuf_Size;
    uint16_t  Uart_Tx_Read_Idx;
    uint16_t  Uart_Tx_Write_Idx;
    uint8_t   Uart_Tx_Busy;
} COM_TypeDef;

#if USE_USART1
    #define USART1_RxBUF_SIZE       64
    #define USART1_TxBUF_SIZE       32
    #define USART1_BAUD             9600

    extern COM_TypeDef COM1;
#endif

#if USE_USART2
    #define USART2_RxBUF_SIZE       512
    #define USART2_TxBUF_SIZE       512
    #define USART2_BAUD             9600

    extern COM_TypeDef COM2;
#endif

void BSP_UART_Init(COM_TypeDef *COMx);
void BSP_UART_RxStart(COM_TypeDef *COMx);
void BSP_UART_TxStart(COM_TypeDef *COMx);
uint8_t BSP_UART_GetTxBusy(COM_TypeDef *COMx);

uint16_t BSP_UART_SendBuff(COM_TypeDef *COMx, uint8_t *Data, uint16_t Data_Len);
uint8_t BSP_UART_Printf(COM_TypeDef *COMx, const char *Str, ...);

#define     UART1_DIR_TXE()     P55 = 1
#define     UART1_DIR_RXE()     P55 = 0

#endif
